package com.mrzhou.core.tool.utils;

import com.mrzhou.core.tool.constants.StringSymbol;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;

public class StringUtil extends StringUtils {

    /**
     *  target中只要包含strings中的任意字符, 那么就放回true
     */
    public static boolean contains(String[] strings, String target) {
        if(null == strings || strings.length <= 0 || StringUtil.isBlank(target)) {
            return false;
        }
        return Stream.of(strings).anyMatch( tr -> target.contains(tr));
    }

    public static String replaceBlank(String str) {
        String dest = "";
        if (str!=null) {
            Pattern p = Pattern.compile("\t|\r|\n");
            Matcher m = p.matcher(str);
            dest = m.replaceAll("  ");
        }
        return dest;
    }

    /**
     * 压缩空格
     * <ul>
     *     <li>StringUtil.compressBlank(xxxxx     xx  xxx   xxx) = xxxxx xx xxx xxx</li>
     * </ul>
     * @param str
     * @return
     */
    public static String compressBlank(String str) {
        if (isBlank(str)) {
            return str;
        }
        return str.replaceAll(" +", " ");
    }

    public static String formatSql(String originSql) {
        if (isBlank(originSql)) {
            return originSql;
        }
        return originSql.replaceAll(",", ", ")
                .replaceAll("\n", StringSymbol.SPACE)
                .replaceAll("  +", StringSymbol.SPACE);
    }

    public static String toStr(Object obj) {
        if (null != obj) {
            return String.valueOf(obj);
        }
        return null;
    }

    /**
     * <pre>
     * StringUtil.isAllBlank(null)             = false
     * StringUtil.isAllBlank(null, "")         = false
     * StringUtil.isAllBlank(new String[] {})  = false
     * StringUtil.isAllBlank(null, "foo")      = false
     * StringUtil.isAllBlank("", "bar")        = false
     * StringUtil.isAllBlank("bob", "")        = false
     * StringUtil.isAllBlank("  bob  ", null)  = false
     * StringUtil.isAllBlank(" ", "bar")       = false
     * StringUtil.isAllBlank("foo", "bar")     = true
     * </pre>
     */
    public static boolean isAllBlank(final CharSequence... css) {
        if (ArrayUtils.isEmpty(css)) {
            return false;
        }
        for (final CharSequence cs : css) {
            if (!isNotBlank(cs)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 检查字符是否为null字符, 不区分大小写
     * <pre>
     * StringUtil.isNullStr(null)      = true
     * StringUtil.isNullStr("")        = true
     * StringUtil.isNullStr(" ")       = true
     * StringUtil.isNullStr("null")  = true
     * StringUtil.isNullStr(" NULL")     = true
     * StringUtil.isNullStr("  bob  ") = false
     * </pre>
     */
    public static boolean isNullStr(String str) {
        return isBlank(str) || "null".equalsIgnoreCase(str.trim());
    }

    /**
     * 创建StringBuilder对象
     *
     * @param sb   初始StringBuilder
     * @param strs 初始字符串列表
     * @return StringBuilder对象
     */
    public static StringBuilder appendBuilder(StringBuilder sb, CharSequence... strs) {
        for (CharSequence str : strs) {
            sb.append(str);
        }
        return sb;
    }

    /**
     * 去掉指定后缀
     *
     * @param str    字符串
     * @param suffix 后缀
     * @return 切掉后的字符串，若后缀不是 suffix， 返回原字符串
     */
    public static String removeSuffix(CharSequence str, CharSequence suffix) {
        if (isEmpty(str) || isEmpty(suffix)) {
            return StringSymbol.EMPTY;
        }

        final String str2 = str.toString();
        if (str2.endsWith(suffix.toString())) {
            return subPre(str2, str2.length() - suffix.length());
        }
        return str2;
    }

    public static String subPre(String source, int length) {
        if(isNotBlank(source)) {
            return source.substring(0, length);
        }
        return source;
    }

}
